﻿using System;
using System.Collections.Generic;
using System.Helpers;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataProcessModule.Protocol
{
    public class SingleBatteryVoltageStats : BaseInstruction
    {
        #region param

        private int SingleBatteriesTotalNumber;//单体蓄电池总数	2	WORD

        private int MotorBatteriesPackNumber;//动力蓄电池包总数N	1	BYTE

        private List<BatteryVoltageInfo> BatteriesVoltageInfos;//电压值列表

        #endregion

        #region attributes

        public int AtSingleBatteriesTotalNumber{ get { return SingleBatteriesTotalNumber; } }

        public int AtMotorBatteriesPackNumber { get { return MotorBatteriesPackNumber; } }

        public List<BatteryVoltageInfo> AtBatteriesVoltageInfos { get { return BatteriesVoltageInfos; } }

        #endregion

        #region construction

        public SingleBatteryVoltageStats()
        {
            instHeadInfo.instID = 0x0113;
        }

        public SingleBatteryVoltageStats(byte[] buf)
        {
            BatteriesVoltageInfos = new List<BatteryVoltageInfo>();
            int offset = 0;
            // 获取头信息
            GetHeadInfo(buf, ref offset);
            BodyStreamSplit(buf, ref offset);
        }

        #endregion

        #region function

        public void BodyStreamSplit(byte[] buf, ref int offset)
        {
            try
            {
                SingleBatteriesTotalNumber = BC.ToInt16Net(buf, ref offset);
                MotorBatteriesPackNumber = buf[offset++];
                for (int i = 0; i < MotorBatteriesPackNumber/2; i++)
                {
                    BatteryVoltageInfo bvi = new BatteryVoltageInfo();
                    bvi.BatterySequnce = buf[offset++];
                    bvi.BatteriesNumber = buf[offset++];
                    for (int j = 0; j < bvi.BatteriesNumber; j++)
                    {
                        bvi.BatteryVoltage += BC.ToUInt16Net(buf, ref offset).ToString() + ',';
                    }
                    BatteriesVoltageInfos.Add(bvi);
                }
            }
            catch (Exception e) { }
        }

        public string CombinedSqlString()
        {
            string _sql = string.Empty;
            _sql = "INSERT INTO table_single_battery_voltage_data (TerminalKey," +
                "SingleBatteriesTotalNumber,MotorBatteriesPackNumber,BatteriesVoltageInfos) VALUES('" +
                instHeadInfo.tdPhoneNumber+"',"+ SingleBatteriesTotalNumber + ',' +
                MotorBatteriesPackNumber + ",'" + ListToString() + "') ON DUPLICATE " +
                "KEY UPDATE SingleBatteriesTotalNumber=VALUES(SingleBatteriesTotalNumber)" +
                ",MotorBatteriesPackNumber=VALUES(MotorBatteriesPackNumber),BatteriesVoltageInfos=VALUES(BatteriesVoltageInfos);";
            return _sql;
        }
        public string InsertString()
        {
            string _sql = string.Empty;
            _sql = "INSERT INTO table_single_battery_voltage_data_" + DateTime.Now.ToString("yyyyMMdd") + " (TerminalKey," +
                         "SingleBatteriesTotalNumber,MotorBatteriesPackNumber,BatteriesVoltageInfos) VALUES('" +
                         instHeadInfo.tdPhoneNumber + "'," + SingleBatteriesTotalNumber + ',' +
                         MotorBatteriesPackNumber + ",'" + ListToString() + "');";
            return _sql;
        }
        public string ListToString()
        {
            string _str = string.Empty;
            foreach (BatteryVoltageInfo bvi in BatteriesVoltageInfos)
            {
                _str += bvi.BatterySequnce.ToString() + '_' + bvi.BatteriesNumber.ToString() + ':' + bvi.BatteryVoltage;
            }
            return _str;
        }

        #endregion
    }

    #region structs

    [Serializable]
    public struct BatteryVoltageInfo
    {
        public int BatterySequnce;

        public int BatteriesNumber;

        public string BatteryVoltage;
    }

    #endregion
}
